using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Events : MyPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "Events";

        if (Session["us_id"] == null)
        {
            Response.Redirect("Login.aspx");
            Response.End();
        }
        if (!Page.IsPostBack)
        {
            if (grdEvents.Attributes["SortExpression"] == null)
            {
                grdEvents.Attributes["SortExpression"] = "ev_id";
                grdEvents.Attributes["SortDirection"] = "DESC";
            }
            BindData();
        }
        SqlDataSource1.InsertCommand = String.Format("INSERT INTO cm_events (ev_type, ev_date, ev_long, ev_lat, ev_title, ev_text, ev_source, ev_status, ev_owner) VALUES (@ev_type, @ev_date, @ev_long, @ev_lat, @ev_title, @ev_text, @ev_source, 0, {0})", Session["us_id"]);
    }

    protected void FormView1_upd(Object sender, FormViewUpdatedEventArgs e)
    {
        BindData();
    }

    protected void FormView1_ins(Object sender,
        FormViewInsertedEventArgs e)
    {
        BindData();
    }

    protected void grdEventsCommand(Object sender, GridViewSelectEventArgs e)
    {
        SqlDataSource1.SelectCommand = "SELECT * FROM cm_events WHERE ev_id = " + ((LinkButton)(this.grdEvents.Rows[e.NewSelectedIndex].Cells[0].Controls[0])).Text;
        FormView1.ChangeMode(FormViewMode.Edit);
    }

    protected void grdEventsDelete(Object sender, GridViewDeleteEventArgs e)
    {
        SqlCommand sp = new SqlCommand("cm_deleteEvent", conn);
            sp.CommandType = CommandType.StoredProcedure;
            sp.Parameters.AddWithValue("@ev_id", Convert.ToInt32(((LinkButton)(this.grdEvents.Rows[e.RowIndex].Cells[0].Controls[0])).Text));
        conn.Open();
        sp.ExecuteNonQuery();
        conn.Close();
        BindData();
    }

    protected void grdPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdEvents.PageIndex = e.NewPageIndex;
        //grdEvents.DataSource = GetDataSource();
        BindData();
    }

    protected void grdEventsDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ev_status")) == 1)
            {
                e.Row.BackColor = System.Drawing.Color.LightGray;
            }
            if (DataBinder.Eval(e.Row.DataItem, "ev_title").ToString().Length > 65)
            {
                e.Row.Cells[3].Text = DataBinder.Eval(e.Row.DataItem, "ev_title").ToString().Substring(0, 65) + "...";
            }
        }
    }


    protected void BindData()
    {
        SqlCommand sp = new SqlCommand("cm_GetEvents", conn);
            sp.CommandType = CommandType.StoredProcedure;
            sp.Parameters.AddWithValue("@User", Session["us_id"]);
            sp.Parameters.AddWithValue("@ReturnAll", 1);
        DataSet ds = new DataSet();
        SqlDataAdapter adp = new SqlDataAdapter(sp);
        adp.Fill(ds, "cm_events");
        conn.Open();
        sp.ExecuteNonQuery();
        DataView dv = new DataView(ds.Tables[0]);

        string sortExpression = grdEvents.Attributes["SortExpression"].ToString();
        string sortDirection = grdEvents.Attributes["SortDirection"].ToString();
        dv.Sort = sortExpression + " " + sortDirection;

        grdEvents.DataSource = dv;
        grdEvents.DataBind();
        conn.Close();
    }

    public void SortGrdEvents(Object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        string sortDirection = "ASC";
        if (sortExpression.Equals(grdEvents.Attributes["SortExpression"].ToString()))
        {
            sortDirection = (grdEvents.Attributes["SortDirection"].ToString().StartsWith("ASC")) ? "DESC" : "ASC";
        }
        grdEvents.Attributes["SortExpression"] = sortExpression;
        grdEvents.Attributes["SortDirection"] = sortDirection;
        BindData();
    }


}
